﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace SPAS
{
    #region 杨斯淋编写
    public partial class AttributeForm : Form
    {

        // 要查询的属性图层
        private IFeatureLayer _curFeatureLayer;

        public AttributeForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设定当前查询图层
        /// </summary>
        public IFeatureLayer CurFeatureLayer
        {
            get { return _curFeatureLayer; }
            set { _curFeatureLayer = value; }
        }


        public void InitTable()
        {
            if (_curFeatureLayer == null) return;

            IFeature feature = null;
            DataTable featureDataTable = new DataTable(); // 创建数据表
            DataRow dataRow = null; // 数据表行变量
            DataColumn dataCol = null; // 数据表列变量
            IField field = null;
            for (int i = 0; i < _curFeatureLayer.FeatureClass.Fields.FieldCount; i++)
            {
                dataCol = new DataColumn();
                field = _curFeatureLayer.FeatureClass.Fields.get_Field(i);
                dataCol.ColumnName = field.AliasName; // 获取字段名作为列标题
                dataCol.DataType = Type.GetType("System.Object");// 定义列字段类型
                featureDataTable.Columns.Add(dataCol); // 在数据表中添加字段信息
            }

            IFeatureCursor featureCursor = _curFeatureLayer.Search(null, true);
            feature = featureCursor.NextFeature();
            while (feature != null)
            {
                dataRow = featureDataTable.NewRow();
                // 获取字段属性
                for (int k = 0; k < featureDataTable.Columns.Count; k++)
                {
                    dataRow[k] = feature.get_Value(k);
                }

                featureDataTable.Rows.Add(dataRow); // 在数据表中添加字段属性信息
                feature = featureCursor.NextFeature();
            }

            // 手动释放com对象
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);

            dataGridViewAttribute.DataSource = featureDataTable;
        }

    }
    #endregion
}
